package hkust.ee.fyp.tool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.List;
import java.util.Vector;

import android.net.Uri;


public class Directory {
	Vector<Uri> imageList;
	Vector<String> imageNameList;
	Vector<Uri> thumbnailList;
	Vector<File> directoryList;
	File currentPath;
	boolean noImage = true;

	public Directory(File path) {
		currentPath = path;
		imageList = new Vector<Uri>();
		imageNameList = new Vector<String>();
		thumbnailList = new Vector<Uri>();
		directoryList = new Vector<File>();
	}

	public void addImage(File file) {
		imageList.add(Uri.parse(file.toString()));
		String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
		imageNameList.add(fileName);
		thumbnailList.add(Uri.parse(currentPath.getPath() +"/.thumbnails/" + fileName));
		noImage = false;
	}

	public void addDir(File file) {
		directoryList.add(file);
	}

	public List<Uri> getImageList() {
		return imageList;
	}
	
	public List<Uri> getThumbnaiList() {
		return thumbnailList;
	}

	public List<File> getDirectoryList() {
		return directoryList;
	}
	
	public List<String> getImageNameList(){
		return imageNameList;
	}

	public File getCurrentPath() {
		return currentPath;
	}

	public String getCurrentName() {
		return currentPath.getName();
	}

	public boolean noImageDirecory() {
		return noImage;
	}

	public static void recursiveSearchDir(Vector<Directory> imageDirecory,
			File path) {
		Directory dir = getDirectoryData(path);
		List<File> list = dir.getDirectoryList();
		if (list.size() > 0) {
			for (File file : list) {
				recursiveSearchDir(imageDirecory, file);
			}
		}
		if (!dir.noImageDirecory()) {
			imageDirecory.add(dir);
		}

	}

	public static Directory getDirectoryData(File path) {
		Directory dir = new Directory(path);

		File[] noMedia = path.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return (name.compareToIgnoreCase(".nomedia") == 0);
			}

		});

		if (noMedia != null) {
			if (noMedia.length == 0) {
				File[] images = path.listFiles(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						return ((name.endsWith(".jpg")) || (name
								.endsWith(".png")));
					}

				});

				for (int i = 0; i < images.length; i++) {					
					dir.addImage(images[i]);
				}

				File[] dirList = path.listFiles(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						return (dir.isDirectory() && !name.contains("."));
					}

				});

				for (int i = 0; i < dirList.length; i++) {
					dir.addDir(dirList[i]);
				}
			}
		}

		return dir;
	}
	
	public static boolean deleteRecursive(File path) throws FileNotFoundException{
	    if (!path.exists()) throw new FileNotFoundException(path.getAbsolutePath());
	    boolean ret = true;
	    if (path.isDirectory()){
	        for (File f : path.listFiles()){
	            ret = ret && deleteRecursive(f);
	        }
	    }
	    return ret && path.delete();
	}
}
